﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using SHomeWorkshop.LunarMind.Converts;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 复制图像到剪贴板命令。
    /// ★①，修改类名。
    /// </summary>
    public static class CopyAsImageToClipBoardCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static CopyAsImageToClipBoardCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "CopyAsImageToClipBoardCommand",
                "CopyAsImageToClipBoardCommand",
                typeof(CopyAsImageToClipBoardCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(
                        Key.C,
                        ModifierKeys.Control | ModifierKeys.Shift,
                        "Ctrl+Shift+C"
                        )
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            CopyAsImageToClipBoard();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void CopyAsImageToClipBoard()
        {
            double scale = ScaleValueConverter.GetNewScaleValue(Globals.MainWindow.sliderOfCanvasScale.Value);

            RenderTargetBitmap rtb = new RenderTargetBitmap(
                (int)(Globals.MainWindow.mainCanvas.Width * scale) + 2,
                (int)(Globals.MainWindow.mainCanvas.Height * scale) + 2,
                96, 96, PixelFormats.Pbgra32);

            rtb.Render(Globals.MainWindow.mainCanvas);

            Clipboard.SetImage(rtb);

            MessageBox.Show("已将文档图片复制到剪贴板！", Globals.AppName,
                MessageBoxButton.OK, MessageBoxImage.Information);
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = true;//无法实现只复制一个标题及其分支。只能整个儿复制。
        }
    }
}
